{
  "cells": [
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "a3e6b1da",
      "metadata": {
        "id": "a3e6b1da"
      },
      "source": [
        "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/pinecone-io/examples/blob/master/docs/quick-tour/interacting-with-the-index.ipynb) [![Open nbviewer](https://raw.githubusercontent.com/pinecone-io/examples/master/assets/nbviewer-shield.svg)](https://nbviewer.org/github/pinecone-io/examples/blob/master/docs/quick-tour/interacting-with-the-index.ipynb)"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "forbidden-sunglasses",
      "metadata": {
        "id": "forbidden-sunglasses",
        "papermill": {
          "duration": 0.053663,
          "end_time": "2021-04-19T21:50:14.234513",
          "exception": false,
          "start_time": "2021-04-19T21:50:14.180850",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "# Interacting with a Pinecone index\n",
        "\n",
        "Pinecone creates an index for your input vectors,\n",
        "and it lets you query their nearest neighbors.\n",
        "A Pinecone index supports the following operations:\n",
        "\n",
        "* `upsert`: insert data formatted as `(id, vector)` tuples into the index, or replace existing `(id, vector)` tuples with new vector values. Optionally, you can attach metadata for each vector so you can use them in the query by specifying conditions. The upserted vector will look like `(id, vector, metadata)`.\n",
        "* `delete`: delete vectors by id.\n",
        "* `query`: query the index and retrieve the top-k nearest neighbors based on dot-product, cosine-similarity, Euclidean distance, and more.\n",
        "* `fetch`: fetch vectors stored in the index by id.\n",
        "* `describe_index_stats`: get statistics about the index."
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "quiet-signal",
      "metadata": {
        "id": "quiet-signal",
        "papermill": {
          "duration": 0.035514,
          "end_time": "2021-04-19T21:50:14.305493",
          "exception": false,
          "start_time": "2021-04-19T21:50:14.269979",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "## Prerequisites"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "beautiful-paper",
      "metadata": {
        "id": "beautiful-paper",
        "papermill": {
          "duration": 0.032332,
          "end_time": "2021-04-19T21:50:14.372018",
          "exception": false,
          "start_time": "2021-04-19T21:50:14.339686",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "Install dependencies."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "id": "complex-diversity",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "execution": {
          "iopub.execute_input": "2021-04-19T21:50:14.449378Z",
          "iopub.status.busy": "2021-04-19T21:50:14.448586Z",
          "iopub.status.idle": "2021-04-19T21:50:30.816337Z",
          "shell.execute_reply": "2021-04-19T21:50:30.814914Z"
        },
        "id": "complex-diversity",
        "outputId": "be409428-838c-49ad-f6cc-5fd1988fb142",
        "papermill": {
          "duration": 16.408105,
          "end_time": "2021-04-19T21:50:30.816847",
          "exception": false,
          "start_time": "2021-04-19T21:50:14.408742",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [],
      "source": [
        "!pip install -qU \\\n",
        "  pinecone-client==3.1.0 \\\n",
        "  pandas==2.0.3"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "dynamic-salad",
      "metadata": {
        "id": "dynamic-salad",
        "papermill": {
          "duration": 0.035091,
          "end_time": "2021-04-19T21:50:30.895993",
          "exception": false,
          "start_time": "2021-04-19T21:50:30.860902",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "Set up Pinecone."
      ]
    },
    {
      "cell_type": "markdown",
      "id": "e7581ed3",
      "metadata": {},
      "source": [
        "Before getting started, decide whether to use serverless or pod-based index."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "a366178e",
      "metadata": {},
      "outputs": [],
      "source": [
        "import os\n",
        "\n",
        "use_serverless = os.environ.get(\"USE_SERVERLESS\", \"False\").lower() == \"true\""
      ]
    },
    {
      "cell_type": "markdown",
      "id": "4b7eca35",
      "metadata": {},
      "source": [
        "## Creating an Index\n",
        "\n",
        "Now the data is ready, we can set up our index to store it.\n",
        "\n",
        "We begin by initializing our connection to Pinecone. To do this we need a [free API key](https://app.pinecone.io)."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "296b4b28",
      "metadata": {},
      "outputs": [],
      "source": [
        "from pinecone import Pinecone\n",
        "\n",
        "# initialize connection to pinecone (get API key at app.pc.io)\n",
        "api_key = os.environ.get('PINECONE_API_KEY') or 'PINECONE_API_KEY'\n",
        "environment = os.environ.get('PINECONE_ENVIRONMENT') or 'PINECONE_ENVIRONMENT'\n",
        "\n",
        "# configure client\n",
        "pc = Pinecone(api_key=api_key)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "7193ae12",
      "metadata": {},
      "source": [
        "Now we setup our index specification, this allows us to define the cloud provider and region where we want to deploy our index. You can find a list of all [available providers and regions here](https://docs.pinecone.io/docs/projects)."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "07826c0c",
      "metadata": {},
      "outputs": [],
      "source": [
        "from pinecone import ServerlessSpec, PodSpec\n",
        "\n",
        "if use_serverless:\n",
        "    spec = ServerlessSpec(cloud='aws', region='us-west-2')\n",
        "else:\n",
        "    spec = PodSpec(environment=environment)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "id": "MjzMwddcyHM2",
      "metadata": {
        "id": "MjzMwddcyHM2",
        "tags": [
          "parameters"
        ]
      },
      "outputs": [],
      "source": [
        "index_name = \"interacting-with-the-index\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "id": "progressive-blues",
      "metadata": {
        "execution": {
          "iopub.execute_input": "2021-04-19T21:50:32.153266Z",
          "iopub.status.busy": "2021-04-19T21:50:32.152565Z",
          "iopub.status.idle": "2021-04-19T21:51:01.609513Z",
          "shell.execute_reply": "2021-04-19T21:51:01.607718Z"
        },
        "id": "progressive-blues",
        "papermill": {
          "duration": 29.494762,
          "end_time": "2021-04-19T21:51:01.610131",
          "exception": false,
          "start_time": "2021-04-19T21:50:32.115369",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [],
      "source": [
        "import time\n",
        "\n",
        "# Delete index if exists\n",
        "if index_name in pc.list_indexes().names():\n",
        "    pc.delete_index(index_name)\n",
        "\n",
        "# Create index\n",
        "pc.create_index(\n",
        "    name=index_name, \n",
        "    dimension=2, \n",
        "    metric=\"euclidean\",\n",
        "    spec=spec\n",
        ")\n",
        "\n",
        "# wait for index to be ready before connecting\n",
        "while not pc.describe_index(index_name).status['ready']:\n",
        "    time.sleep(1)\n",
        "\n",
        "# Connect to the index\n",
        "index = pc.Index(index_name)"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "billion-imperial",
      "metadata": {
        "id": "billion-imperial",
        "papermill": {
          "duration": 0.043379,
          "end_time": "2021-04-19T21:51:01.699928",
          "exception": false,
          "start_time": "2021-04-19T21:51:01.656549",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "### Insert vectors\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "id": "analyzed-charity",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 206
        },
        "execution": {
          "iopub.execute_input": "2021-04-19T21:51:01.783626Z",
          "iopub.status.busy": "2021-04-19T21:51:01.782872Z",
          "iopub.status.idle": "2021-04-19T21:51:01.988911Z",
          "shell.execute_reply": "2021-04-19T21:51:01.988361Z"
        },
        "id": "analyzed-charity",
        "outputId": "e6797ecc-f731-40e0-c4bf-a1bc409289dd",
        "papermill": {
          "duration": 0.249711,
          "end_time": "2021-04-19T21:51:01.989157",
          "exception": false,
          "start_time": "2021-04-19T21:51:01.739446",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "\n",
              "\n",
              "  <div id=\"df-cab99c01-f743-4676-9966-b513a4e8f97f\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>id</th>\n",
              "      <th>vector</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>A</td>\n",
              "      <td>[1.0, 1.0]</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>B</td>\n",
              "      <td>[2.0, 2.0]</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>C</td>\n",
              "      <td>[3.0, 3.0]</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>D</td>\n",
              "      <td>[4.0, 4.0]</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>E</td>\n",
              "      <td>[5.0, 5.0]</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-cab99c01-f743-4676-9966-b513a4e8f97f')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "\n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "\n",
              "\n",
              "\n",
              "    <div id=\"df-2c35fefa-6124-44b5-b8ad-cb54c4fa55fb\">\n",
              "      <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-2c35fefa-6124-44b5-b8ad-cb54c4fa55fb')\"\n",
              "              title=\"Suggest charts.\"\n",
              "              style=\"display:none;\">\n",
              "\n",
              "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "     width=\"24px\">\n",
              "    <g>\n",
              "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
              "    </g>\n",
              "</svg>\n",
              "      </button>\n",
              "    </div>\n",
              "\n",
              "<style>\n",
              "  .colab-df-quickchart {\n",
              "    background-color: #E8F0FE;\n",
              "    border: none;\n",
              "    border-radius: 50%;\n",
              "    cursor: pointer;\n",
              "    display: none;\n",
              "    fill: #1967D2;\n",
              "    height: 32px;\n",
              "    padding: 0 0 0 0;\n",
              "    width: 32px;\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart:hover {\n",
              "    background-color: #E2EBFA;\n",
              "    box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "    fill: #174EA6;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart {\n",
              "    background-color: #3B4455;\n",
              "    fill: #D2E3FC;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart:hover {\n",
              "    background-color: #434B5C;\n",
              "    box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "    filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "    fill: #FFFFFF;\n",
              "  }\n",
              "</style>\n",
              "\n",
              "    <script>\n",
              "      async function quickchart(key) {\n",
              "        const containerElement = document.querySelector('#' + key);\n",
              "        const charts = await google.colab.kernel.invokeFunction(\n",
              "            'suggestCharts', [key], {});\n",
              "      }\n",
              "    </script>\n",
              "\n",
              "      <script>\n",
              "\n",
              "function displayQuickchartButton(domScope) {\n",
              "  let quickchartButtonEl =\n",
              "    domScope.querySelector('#df-2c35fefa-6124-44b5-b8ad-cb54c4fa55fb button.colab-df-quickchart');\n",
              "  quickchartButtonEl.style.display =\n",
              "    google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "}\n",
              "\n",
              "        displayQuickchartButton(document);\n",
              "      </script>\n",
              "      <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-cab99c01-f743-4676-9966-b513a4e8f97f button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-cab99c01-f743-4676-9966-b513a4e8f97f');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n"
            ],
            "text/plain": [
              "  id      vector\n",
              "0  A  [1.0, 1.0]\n",
              "1  B  [2.0, 2.0]\n",
              "2  C  [3.0, 3.0]\n",
              "3  D  [4.0, 4.0]\n",
              "4  E  [5.0, 5.0]"
            ]
          },
          "execution_count": 5,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Generate some data\n",
        "import pandas as pd\n",
        "\n",
        "df = pd.DataFrame()\n",
        "df[\"id\"] = [\"A\", \"B\", \"C\", \"D\", \"E\"]\n",
        "df[\"vector\"] = [[1., 1.], [2., 2.], [3., 3.], [4., 4.], [5., 5.]]\n",
        "df"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "e3c126d0",
      "metadata": {
        "id": "e3c126d0",
        "papermill": {
          "duration": 0.045865,
          "end_time": "2021-04-19T21:51:02.074280",
          "exception": false,
          "start_time": "2021-04-19T21:51:02.028415",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "We perform upsert operations in our index. This call will insert a new vector in the index or update the vector if the id was already present."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "id": "checked-christopher",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "execution": {
          "iopub.execute_input": "2021-04-19T21:51:02.172953Z",
          "iopub.status.busy": "2021-04-19T21:51:02.172114Z",
          "iopub.status.idle": "2021-04-19T21:51:02.615744Z",
          "shell.execute_reply": "2021-04-19T21:51:02.614707Z"
        },
        "id": "checked-christopher",
        "outputId": "2cc949ae-20d5-49e9-e1f5-edc7f6196ecb",
        "papermill": {
          "duration": 0.489967,
          "end_time": "2021-04-19T21:51:02.616195",
          "exception": false,
          "start_time": "2021-04-19T21:51:02.126228",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'upserted_count': 2}"
            ]
          },
          "execution_count": 6,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Upsert the vectors\n",
        "AB_df = df[:2]\n",
        "index.upsert(vectors=zip(AB_df.id, AB_df.vector))"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "psychological-estate",
      "metadata": {
        "id": "psychological-estate",
        "papermill": {
          "duration": 0.041408,
          "end_time": "2021-04-19T21:51:02.709492",
          "exception": false,
          "start_time": "2021-04-19T21:51:02.668084",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "### Fetch vectors"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "id": "varied-scene",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "execution": {
          "iopub.execute_input": "2021-04-19T21:51:02.794505Z",
          "iopub.status.busy": "2021-04-19T21:51:02.793874Z",
          "iopub.status.idle": "2021-04-19T21:51:02.911044Z",
          "shell.execute_reply": "2021-04-19T21:51:02.909592Z"
        },
        "id": "varied-scene",
        "outputId": "8ab7b5af-dcc9-4da2-df38-83fc147fe3ce",
        "papermill": {
          "duration": 0.160346,
          "end_time": "2021-04-19T21:51:02.911383",
          "exception": false,
          "start_time": "2021-04-19T21:51:02.751037",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'namespace': '',\n",
              " 'vectors': {'A': {'id': 'A', 'metadata': {}, 'values': [1.0, 1.0]},\n",
              "             'B': {'id': 'B', 'metadata': {}, 'values': [2.0, 2.0]}}}"
            ]
          },
          "execution_count": 7,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Fetch vectors by ID\n",
        "fetch_results = index.fetch(ids=[\"A\", \"B\"])\n",
        "fetch_results"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "frank-participation",
      "metadata": {
        "id": "frank-participation",
        "papermill": {
          "duration": 0.040162,
          "end_time": "2021-04-19T21:51:02.997106",
          "exception": false,
          "start_time": "2021-04-19T21:51:02.956944",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "### Query top-k vectors"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "id": "dried-demographic",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "execution": {
          "iopub.execute_input": "2021-04-19T21:51:03.087127Z",
          "iopub.status.busy": "2021-04-19T21:51:03.086311Z",
          "iopub.status.idle": "2021-04-19T21:51:03.202008Z",
          "shell.execute_reply": "2021-04-19T21:51:03.200868Z"
        },
        "id": "dried-demographic",
        "outputId": "9a2c2cac-c4b7-4883-c20c-92bd33121096",
        "papermill": {
          "duration": 0.164285,
          "end_time": "2021-04-19T21:51:03.202342",
          "exception": false,
          "start_time": "2021-04-19T21:51:03.038057",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'matches': [{'id': 'A', 'score': 0.0199999809, 'values': []},\n",
              "             {'id': 'B', 'score': 1.61999989, 'values': []}],\n",
              " 'namespace': ''}"
            ]
          },
          "execution_count": 9,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Query top-k nearest neighbors\n",
        "query_results = index.query(vector=[1.1, 1.1], top_k=2)\n",
        "query_results"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "binary-drama",
      "metadata": {
        "id": "binary-drama",
        "papermill": {
          "duration": 0.041674,
          "end_time": "2021-04-19T21:51:03.292154",
          "exception": false,
          "start_time": "2021-04-19T21:51:03.250480",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "### Update vectors by ID"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "id": "generic-witness",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "execution": {
          "iopub.execute_input": "2021-04-19T21:51:03.380443Z",
          "iopub.status.busy": "2021-04-19T21:51:03.379802Z",
          "iopub.status.idle": "2021-04-19T21:51:03.483914Z",
          "shell.execute_reply": "2021-04-19T21:51:03.482535Z"
        },
        "id": "generic-witness",
        "outputId": "c9e0aa85-be77-4a71-833e-87b875962a25",
        "papermill": {
          "duration": 0.150867,
          "end_time": "2021-04-19T21:51:03.484308",
          "exception": false,
          "start_time": "2021-04-19T21:51:03.333441",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'namespace': '',\n",
              " 'vectors': {'A': {'id': 'A', 'metadata': {}, 'values': [1.0, 1.0]}}}"
            ]
          },
          "execution_count": 10,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Fetch current vectors by ID\n",
        "fetch_result = index.fetch(ids=[\"A\"])\n",
        "fetch_result"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "id": "comic-rwanda",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "execution": {
          "iopub.execute_input": "2021-04-19T21:51:03.583334Z",
          "iopub.status.busy": "2021-04-19T21:51:03.582536Z",
          "iopub.status.idle": "2021-04-19T21:51:03.682411Z",
          "shell.execute_reply": "2021-04-19T21:51:03.681534Z"
        },
        "id": "comic-rwanda",
        "outputId": "0c2a09c9-4ce8-4a21-81da-448d1aae20fc",
        "papermill": {
          "duration": 0.148497,
          "end_time": "2021-04-19T21:51:03.683025",
          "exception": false,
          "start_time": "2021-04-19T21:51:03.534528",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'upserted_count': 1}"
            ]
          },
          "execution_count": 11,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Update vectors by ID\n",
        "index.upsert(vectors=[(\"A\",[0.1, 0.1])])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "id": "gentle-messenger",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "execution": {
          "iopub.execute_input": "2021-04-19T21:51:03.789552Z",
          "iopub.status.busy": "2021-04-19T21:51:03.787734Z",
          "iopub.status.idle": "2021-04-19T21:51:03.891402Z",
          "shell.execute_reply": "2021-04-19T21:51:03.890004Z"
        },
        "id": "gentle-messenger",
        "outputId": "0b4c6184-45bb-41ff-fadd-eef7074023f0",
        "papermill": {
          "duration": 0.161304,
          "end_time": "2021-04-19T21:51:03.892409",
          "exception": false,
          "start_time": "2021-04-19T21:51:03.731105",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'namespace': '',\n",
              " 'vectors': {'A': {'id': 'A', 'metadata': {}, 'values': [0.0, 0.0]}}}"
            ]
          },
          "execution_count": 12,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Fetch vector by the same ID again\n",
        "fetch_result = index.fetch(ids=[\"A\"])\n",
        "fetch_result"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "manual-format",
      "metadata": {
        "id": "manual-format",
        "papermill": {
          "duration": 0.04065,
          "end_time": "2021-04-19T21:51:03.983746",
          "exception": false,
          "start_time": "2021-04-19T21:51:03.943096",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "### Delete vectors by ID"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "id": "hispanic-talent",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "execution": {
          "iopub.execute_input": "2021-04-19T21:51:04.070560Z",
          "iopub.status.busy": "2021-04-19T21:51:04.069790Z",
          "iopub.status.idle": "2021-04-19T21:51:04.172448Z",
          "shell.execute_reply": "2021-04-19T21:51:04.171654Z"
        },
        "id": "hispanic-talent",
        "outputId": "370409af-beae-4d47-91ab-beaadb10041b",
        "papermill": {
          "duration": 0.14878,
          "end_time": "2021-04-19T21:51:04.172749",
          "exception": false,
          "start_time": "2021-04-19T21:51:04.023969",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{}"
            ]
          },
          "execution_count": 13,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Delete vectors by ID\n",
        "index.delete(ids=[\"A\"])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 14,
      "id": "romantic-dubai",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "execution": {
          "iopub.execute_input": "2021-04-19T21:51:04.270395Z",
          "iopub.status.busy": "2021-04-19T21:51:04.269046Z",
          "iopub.status.idle": "2021-04-19T21:51:04.379679Z",
          "shell.execute_reply": "2021-04-19T21:51:04.378909Z"
        },
        "id": "romantic-dubai",
        "outputId": "3fa24e74-24fa-4009-c406-791e85451c9d",
        "papermill": {
          "duration": 0.161047,
          "end_time": "2021-04-19T21:51:04.380132",
          "exception": false,
          "start_time": "2021-04-19T21:51:04.219085",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'namespace': '',\n",
              " 'vectors': {'B': {'id': 'B', 'metadata': {}, 'values': [2.0, 2.0]}}}"
            ]
          },
          "execution_count": 14,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Deleted vectors are empty\n",
        "fetch_results = index.fetch(ids=[\"A\", \"B\"])\n",
        "fetch_results"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "balanced-intellectual",
      "metadata": {
        "id": "balanced-intellectual",
        "papermill": {
          "duration": 0.044809,
          "end_time": "2021-04-19T21:51:04.471991",
          "exception": false,
          "start_time": "2021-04-19T21:51:04.427182",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "### Get index statistics"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "id": "nonprofit-popularity",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "execution": {
          "iopub.execute_input": "2021-04-19T21:51:04.567713Z",
          "iopub.status.busy": "2021-04-19T21:51:04.566158Z",
          "iopub.status.idle": "2021-04-19T21:51:04.672337Z",
          "shell.execute_reply": "2021-04-19T21:51:04.671491Z"
        },
        "id": "nonprofit-popularity",
        "outputId": "2887c3e9-1820-4e40-9869-80500295bdf7",
        "papermill": {
          "duration": 0.154298,
          "end_time": "2021-04-19T21:51:04.672703",
          "exception": false,
          "start_time": "2021-04-19T21:51:04.518405",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'dimension': 2,\n",
              " 'index_fullness': 1e-05,\n",
              " 'namespaces': {'': {'vector_count': 1}},\n",
              " 'total_vector_count': 1}"
            ]
          },
          "execution_count": 15,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Index statistics\n",
        "index.describe_index_stats()"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "directed-keyboard",
      "metadata": {
        "id": "directed-keyboard",
        "papermill": {
          "duration": 0.043493,
          "end_time": "2021-04-19T21:51:04.764495",
          "exception": false,
          "start_time": "2021-04-19T21:51:04.721002",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "### Delete the index"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 16,
      "id": "supported-casino",
      "metadata": {
        "execution": {
          "iopub.execute_input": "2021-04-19T21:51:04.861077Z",
          "iopub.status.busy": "2021-04-19T21:51:04.859360Z",
          "iopub.status.idle": "2021-04-19T21:51:17.500278Z",
          "shell.execute_reply": "2021-04-19T21:51:17.500861Z"
        },
        "id": "supported-casino",
        "papermill": {
          "duration": 12.691337,
          "end_time": "2021-04-19T21:51:17.501135",
          "exception": false,
          "start_time": "2021-04-19T21:51:04.809798",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [],
      "source": [
        "# delete the index\n",
        "pc.delete_index(index_name)"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.11"
    },
    "papermill": {
      "default_parameters": {},
      "duration": 64.825822,
      "end_time": "2021-04-19T21:51:18.073345",
      "environment_variables": {},
      "exception": null,
      "input_path": "/notebooks/quick_tour/interacting_with_the_index.ipynb",
      "output_path": "/notebooks/tmp/quick_tour/interacting_with_the_index.ipynb",
      "parameters": {},
      "start_time": "2021-04-19T21:50:13.247523",
      "version": "2.3.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
